package com.clss.commandhelper.function.newEvidence;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.ImageView;

import com.clss.commandhelper.R;
import com.clss.commandhelper.tools.baseRvAdapter.AutoRVAdapter;
import com.clss.commandhelper.tools.baseRvAdapter.ViewHolder;
import com.clss.commandhelper.view.CountDownTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/16.
 */

public class TaskAudioAdapter extends AutoRVAdapter {
    int max = 9;
    private List<Integer> itemState;
    public final static int stop = 0;
    public final static int start = 1;

    public void setMax(int max) {
        this.max = max;
    }

    public TaskAudioAdapter(Context context, List<TaskPoliceFile> data) {
        super(context, data);
        itemState = new ArrayList<>();
    }


    public List<Integer> getItemState(){
        return itemState;
    }
    public void removeItemState(int position){
        if (itemState.size() > position){
            itemState.remove(position);
            notifyDataSetChanged();
        }
    }
    public void setItemState(int position,int value){
        itemState.set(position,value);
        notifyItemChanged(position);
    }
    public void setStartToStop(){
        for (int i = 0; i < itemState.size(); i++) {
            if (itemState.get(i) == start){
                itemState.set(i,stop);
                notifyItemChanged(i);
            }
        }
    }

    @Override
    public int onCreateViewLayoutID(int viewType) {
        return R.layout.item_audio;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        TaskPoliceFile taskPoliceFile = (TaskPoliceFile) list.get(position);
        CountDownTextView timeText = holder.get(R.id.item_audio_countdown);
        ImageView img = holder.get(R.id.item_audio_img);
        ImageView delete = holder.get(R.id.item_audio_del);
        timeText.setType(1);
        if (taskPoliceFile.getRecordTimeLength() == 0 ){
            timeText.setVisibility(View.GONE);
        }else {
            timeText.setVisibility(View.VISIBLE);
        }
        timeText.setTime(taskPoliceFile.getRecordTimeLength());

        if (itemState.size() == list.size()){
            if (itemState.get(position) == start){
                img.setImageResource(R.drawable.voice_play);
//                helper.setImageResource(R.id.item_audio_img,R.drawable.voice_play);
//                ImageView imageView = helper.getView(R.id.item_audio_img);
                AnimationDrawable animationDrawable = (AnimationDrawable) img.getDrawable();
                animationDrawable.start();
                timeText.beginRun();
            }else
            if (itemState.get(position) == stop){
                img.setImageResource(R.drawable.voice3);
//                helper.setImageResource(R.id.item_audio_img,);
                timeText.stopRun();
            }
        }else {
            img.setImageResource(R.drawable.voice3);
//            helper.setImageResource(R.id.item_audio_img,R.drawable.voice3);
            itemState.add(stop);
        }
        if (onChildItemClickListener != null){
            delete.setOnClickListener(view -> onChildItemClickListener.onChildItemClick(view,position));
        }
    }

    private OnChildItemClickListener onChildItemClickListener;
    public interface OnChildItemClickListener {
        void onChildItemClick(View view, int position);
    }
    public void setOnChildItemClickListener(OnChildItemClickListener onChildItemClickListener) {
        this.onChildItemClickListener = onChildItemClickListener;
    }
}
